package com.h1.staffmanage.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.h1.commonutils.R;
import com.h1.staffmanage.entity.Company;
import com.h1.staffmanage.service.CompanyService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mengfanyu
 * @since 2022-06-22
 */
@RestController
@CrossOrigin
@RequestMapping("/staffmanage/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;
    @GetMapping("findAllCompany")
    public R findAllCompany() {
        //调用service的方法实现查询所有的操作
        List<Company> list = companyService.list(null);
        return R.ok().data("items",list);
    }

    @DeleteMapping("{id}")
    public R removeCompany(
                           @PathVariable long id) {
        boolean flag = companyService.removeById(id);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    @GetMapping("pageCompany/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit) {
        //创建page对象
        Page<Company> pageCompany = new Page<>(current,limit);


        companyService.page(pageCompany,null);

        long total = pageCompany.getTotal();//总记录数
        List<Company> records = pageCompany.getRecords(); //数据list集合



        return R.ok().data("total",total).data("rows",records);
    }
    //添加讲师接口的方法
    @PostMapping("addCompany")
    public R addTeacher(@RequestBody Company company) {
        boolean save = companyService.save(company);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    @GetMapping("getCompany/{id}")
    public R getTeacher(@PathVariable long id) {
        Company byId = companyService.getById(id);
        return R.ok().data("companybyId",byId);
    }

    @PostMapping("updateCompany")
    public R updateTeacher(@RequestBody Company company) {
        boolean flag = companyService.updateById(company);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}

